package com.ten.course.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ten.common.core.entity.CheckCourse;
import com.ten.common.core.entity.EduCourse;
import com.ten.common.core.entity.EduSubject;
import com.ten.common.core.vo.Result;
import com.ten.course.dao.CourseCheckDao;
import com.ten.course.dao.CourseListDao;
import com.ten.course.dao.CourseSubjectDao;
import com.ten.course.service.CourseCheckService;
import com.ten.course.utils.CourseUtil;
import com.ten.course.vo.CourseCheckVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ：张成（3092211427@qq.com）
 * @date ：Created in 2022/12/7 14:27
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class CourseCheckServiceImpl implements CourseCheckService {

    @Autowired
    private CourseCheckDao courseCheckDao;

    @Autowired
    private CourseSubjectDao courseSubjectDao;

    @Autowired
    private CourseListDao courseListDao;

    @Override
    public Result<IPage<CheckCourse>> findCourseCheck(Integer pageCurrent, Integer pageSize, CourseCheckVo courseCheckVo) {
        IPage<CheckCourse> iPage = new Page<>(pageCurrent, pageSize);
        IPage<CheckCourse> iPage1 = courseCheckDao.findCourseCheck(iPage, courseCheckVo);
        //根据课程名称查询出课程的分类
        List<CheckCourse> records = iPage1.getRecords();
        List<EduSubject> eduSubjects = courseSubjectDao.selectList(null);
        //获取课程分类树型结构
        List<EduSubject> treeEduSubjectList = CourseUtil.fromEduSubjectListToTreeEduSubjectList(eduSubjects);
        //对数据中的课程信息添加带有所属课程分类层级subjectName属性字段
        EduSubject eduSubject = new EduSubject();
        for (CheckCourse checkCourse : records) {
            //返回本级的EduSubject
            eduSubject = findOneself(eduSubject, treeEduSubjectList, checkCourse);
            StringBuilder sb = new StringBuilder();
            String subjectName = getSubjectName(eduSubject, sb).toString();
            checkCourse.setSubjectName(subjectName);
        }
        return new Result<>(200, "查询成功", iPage1);
    }

    /**
     * 根据课程分类父id来判断是否拼接下一个孩子字段
     * @param eduSubject
     * @param sb
     * @return
     */
    private StringBuilder getSubjectName(EduSubject eduSubject, StringBuilder sb) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(("【")).append(eduSubject.getTitle()).append("】");
        if (!eduSubject.getParentId().equals("0")) {
            EduSubject eduSubject1 = courseSubjectDao.selectById(eduSubject.getParentId());
            sb = getSubjectName(eduSubject1, sb);
            sb.append(" >");
        }
        return sb.append(stringBuilder);
    }

    /**
     * 根据checkCourse查询出普通的eduSubject所在树型结构中位置中的eduSubject
     * @param eduSubject
     * @param treeEduSubjectList
     * @param checkCourse
     * @return
     */
    private EduSubject findOneself(EduSubject eduSubject, List<EduSubject> treeEduSubjectList, CheckCourse checkCourse) {
        for (EduSubject Subject : treeEduSubjectList) {
            if (Subject.getId().equals(checkCourse.getSubjectId())) {
                return Subject;
            } else {
                List<EduSubject> children = Subject.getChildren();
                eduSubject = findOneself(eduSubject, children, checkCourse);
            }
        }
        return eduSubject;
    }

    /**
     * 根据课程id修改审核的状态
     *
     * @param checkCourse 课程审核类的所有数据
     * @return
     */
    @Transactional
    @Override
    public Result updateCourseCheck(CheckCourse checkCourse) {
        int i = courseCheckDao.updateById(checkCourse);
        if (i > 0 && checkCourse.getCheckStatus().equals("1")) {
            //审核过后，对课程的status进行修改，审核通过后直接进行上架
            EduCourse eduCourse = courseListDao.selectById(checkCourse.getCourseId());
            eduCourse.setStatus("Normal");
            int i1 = courseListDao.updateById(eduCourse);
            if (i1 > 0) {
                return new Result(200, "审核通过");
            }
        }
        return new Result(500, "审核不通过");
    }


    /*************************客户端service*********************/
}